define(["dojo/_base/declare", "dojo/_base/lang", "esri/tasks/QueryTask", "esri/tasks/query", "dojo/_base/array", "dojo/string"],
	function(declare, lang, QueryTask, Query, array, string) {
		return declare(null, {
			queryTask: null,
			query: null,
			url: "",
			outFields: ["*"],
			returnGeometry: true,
			geometry: null,
		//	fields: ["*"],
			spatialRelationship :Query.SPATIAL_REL_ENVELOPEINTERSECTS,
			constructor: function(params) {
				if(params) {
					lang.mixin(this, params);
				}
				this.initQueryTask();
				this.initQuery();
			},
			/**
			 * @summary 初始化queryTask对象；
			 */
			initQueryTask: function() {
				var url = this.url;
				if(typeof url == "string" && url != "") {
					this.queryTask = new QueryTask(this.url);
				} else {
					console.log("'url'参数错误！");
				}

			},
			/**
			 * @summary 初始化查询参数
			 */
			initQuery: function() {
				var geometry = this.geometry,
				spatialRelationship = this.spatialRelationship,
					where = "";
				this.query = new Query();
				where = this.constructWhere();
				if(where != "") {
					this.query.where = where;
				}
				if(geometry) {
					this.query.geometry = geometry;
				}
				if(spatialRelationship){
					this.query.spatialRelationship = Query[spatialRelationship];
				}
				this.query.outFields = this.outFields;
				this.query.returnGeometry = this.returnGeometry;
			},
			/**
			 * @summary 根据fields参数构建where语句
			 */
			constructWhere: function() {
				var fields = this.fields,
					where = "",
					isFirst = true;
				array.forEach(fields, function(field) {
					var name = field.name,
						value = field.value,
						fieldTemplate = "",
						fieldWhere = "";
					if(value != "") {
						if(lang.isArray(value)) {
							fieldTemplate="${name} in (${value})";
							fieldWhere = string.substitute(fieldTemplate, {
								name: name,
								value: value.toString()
							});
							//fieldWhere=name+" in ("+value.toString()+")";
						} else {
							if(typeof value == "string") {
								if( name == "GJZ") {
									fieldTemplate = "${name} like '%${value}%'";
								}else {
									fieldTemplate = "${name}='${value}'";
								}

							} else if(typeof value == "number") {
								fieldTemplate = "${name}=${value}";
							}
							fieldWhere = string.substitute(fieldTemplate, {
                name: name,
                value: value
              });
						}
						if(isFirst) {
							where = fieldWhere;
							isFirst = false;
						} else {
							where += " and " + fieldWhere;
						}
					}
				});
				where = where ? where : "1=1";
				return where;
			},

			/**
			 * @summary 根据条件获取要素
			 */
			execute: function() {
				return this.queryTask.execute(this.query);
			},
			/**
			 * @summary 根据ids获取要素
			 * @param {Object} objectIds
			 */
			executebyObjectIds: function(objectIds) {
				if(lang.isArray(objectIds) && objectIds.length > 0) {
					this.query.objectIds = objectIds;
					return this.queryTask.execute(this.query);
				} else {
					console.log("objectIds参数不正确！");
				}

			},
			/**
			 * @summary 根据条件，返回查询的ids
			 */
			executeForIds: function() {
				return this.queryTask.executeForIds(this.query);
			},
			/**
			 * @当查询参数改变时 修改参数值
			 */
			setFields: function(data) {
				this.fields = data;
				this.initQuery();
			}
		});
	});
